package cn.edu.ljl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;


//对学习建议信息进行修改，提供修改界面，在输入框中已有原数据信息



public class overwrite extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String sql = "SELECT * FROM grade WHERE id=" + id;
        System.out.println(sql);

        String name = judge.Name;
        String advice = judge.Advice;
        int score = 0;
        String subject = null;

        try {
            List<Student> students = DBMS.getStudents(sql);
            Student student = students.get(0);
            score = student.getScore();
            subject = student.getSubject();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        System.out.println(name + "  " + subject + "  " + advice + "  " + score);


        if (score >= 85) {

            StringBuilder sb = new StringBuilder();
            sb.append(Page.getHeader());
            sb.append(Page.getPic());
            sb.append(buildAdviceForm(name, subject, advice));
            sb.append(Page.getEnd());

            response.setCharacterEncoding("UTF-8");
            Writer writer = response.getWriter();
            writer.write(sb.toString());
        }else {
            Writer writer = response.getWriter();
            StringBuilder sb = new StringBuilder();
            sb.append(Page.getHeader());
            sb.append(Page.getPic());
            sb.append(judge.toHtml());
            sb.append(Page.getEnd());
            writer.write(sb.toString());
        }
    }

    public String buildAdviceForm(String name, String subject, String advice) {
        String html = String.format("<center>\n" +
                        "<br>\n" +
                        "<form action='./getOverwrite' method='POST''>\n" +
                        "<table style='background-color:#e35100;padding:2em'>\n" +
                        "<tr><td>您的名字:</td><td><font face=\"宋体\" >%s</font></td></tr>\n" +
                        "<tr><td>学科名字:</td><td><font face=\"宋体\" >%s</font></td></tr>\n" +
                        "<tr><td>您的建议:</td><td><textarea name=advice cols=\"50\" rows=\"8\" value=''>%s</textarea></td></tr>\n" +
                        "<tr><td colspan=\"2\" align=\"center\"><br><input type='submit' value=\"添加建议\"></td></tr>\n" +
                        "</table>" +
                        "</form>\n" +
                        "</div>\n" +
                        "</center>",
                name, subject, advice
        );
        return html;
    }

}
